package cn.edu.ujn.controller;

import java.util.List;

import javax.security.auth.message.callback.PrivateKeyCallback.Request;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.dao.User;
import cn.edu.ujn.service.IUserService;

@Controller
public class UserController {
	@Autowired
	private IUserService userService;
	@Autowired
	private User user;

	// 页面跳转
	@RequestMapping(value = "/toLogin")
	public String toLogin(HttpSession session) {
		return "login";

	}

	// 用户退出
	@RequestMapping(value = "/logout")
	public String logout(HttpSession session) {
		// 清除Seesion
		session.invalidate();
		// 重定向到登录页面
		return "redirect:toLogin";
	}

	// 用户登录
	@RequestMapping(value = "/login.action")
	// 传入的参数String usercode,String password
	public String login(String usercode, String password, String userAuthority, Model model, HttpSession session) {
		System.out.println(usercode + "," + password);
		User findUser = userService.findUser(usercode, password);
		System.out.println(findUser);
		if (findUser != null) {
			if (!findUser.getUserState().equals("1")) {
				model.addAttribute("msg", "用户状态异常，请联系管理员！");
				return "login";
			} else {
				if (!findUser.getUserAuthority().equals(userAuthority)) {
					model.addAttribute("msg", "用户身份选择错误，请重新登录！");
					return "login";
				} else {

					session.setAttribute("USER_SESSION", findUser);
					// 重定向到主页,直接显示出分页后的客户列表信息
					if (findUser.getUserAuthority().equals("0"))
						return "redirect:user/find";
					// return "user";
					else {
						return "redirect:/azhao";
					}
				}
			}
		}
		model.addAttribute("msg", "用户名或密码错误，请重新登录！");
		return "login";
	}

	// 注册用户
	@RequestMapping(value = "create.action")
	@ResponseBody
	public String userCreate(User user) {
		user.setUserState("1");
		user.setUserAuthority("1");

		// 执行Service层中的创建方法，返回的是受影响的行数
		System.out.println(user);
		if (userService.selectByCode(user.getUserCode()) != null) {

			return "FAIL";
		}

		else {
			int rows = userService.creatUser(user);

			return "OK";
		}

	}

	// 新建用户
	@RequestMapping(value = "/user/xinjian")
	@ResponseBody
	public String userCreate2(User user) {

		// 执行Service层中的创建方法，返回的是受影响的行数
		System.out.println(user);
		int rows = userService.creatUser2(user);
		System.out.println(rows);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	@RequestMapping(value = "/user/list")
	public String findAllUser(Model model) {
		List<User> findAllUser = userService.findAllUser();
		model.addAttribute("findAllUser", findAllUser);
		// System.out.println(findAllUser);
		return "user";

	}

	// 列表显示 模糊查询
	@RequestMapping(value = "/user/find")
	public String findUserByType(String userCode, String userName, String userAuthority, String gender,
			String userState, Model model) {
		model.addAttribute("userCode", userCode);
		model.addAttribute("userName", userName);
		model.addAttribute("userAuthority", userAuthority);
		model.addAttribute("gender", gender);
		model.addAttribute("userState", userState);
		List<User> findUserByType = userService.findUserByType(userCode, userName, userAuthority, gender, userState);
		model.addAttribute("findUserByType", findUserByType);
		System.out.println(findUserByType);
		return "user";

	}

	// 用户删除
	@RequestMapping(value = "/user/delete")
	@ResponseBody
	public String deleteUsers(Integer id) {
		System.out.println(id);
		int deleteUser = userService.deleteUser(id);
		System.out.println(deleteUser);
		if (deleteUser > 0) {
			return "OK";
		} else {
			return "FAIL";
		}

	}

	// 通过id查询用户
	@RequestMapping("/user/getUserById")
	@ResponseBody
	public User getUserById(Integer id) {
		User selectById = userService.selectById(id);
		System.out.println(selectById);
		return selectById;
	}

//用户信息修改
	@RequestMapping(value = "/user/xiugai")
	@ResponseBody
	public String Updateuser(User user) {
		System.out.println(user);
		int rows = userService.update(user);
		System.out.println(rows);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}

	}

}
